package com.atguigu.crowd.service.impl;

import atguigu.crowd.util.ResultEntity;
import com.atguigu.crowd.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResultEntity<String> setRedisKeyValueRemote(String key, String value) {
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set(key, value);
        return ResultEntity.successWithoutData();
    }

    @Override
    public ResultEntity<String> setRedisKeyValueRemoteWithTimeOut(String key, String value, Long time) {
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set(key, value, time, TimeUnit.SECONDS);
        return ResultEntity.successWithoutData();
    }

    @Override
    public ResultEntity<String> getRedisStringValueByKeyRemote(String key) {
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        String value = operations.get(key);
        return ResultEntity.successWithData(value);
    }

    @Override
    public ResultEntity<String> removeRedisKeyRemote(String key) {
        stringRedisTemplate.delete(key);
        return ResultEntity.successWithoutData();
    }
}
